package com.scpii.universal.pns.dao.impl;

import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.impl.BaseDaoImpl;
import com.scpii.universal.pns.dao.DevicesDao;
import com.scpii.universal.pns.entity.Devices;

@Repository
public class DevicesDaoImpl extends BaseDaoImpl<Devices, Long> implements
		DevicesDao {

	@SuppressWarnings("unchecked")
	@Override
	public String[] getDevicesModels(Long appId) {
		String schema = ResourceBundle.getBundle(
				GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault())
				.getString("pns.schema");
		String sql = MessageFormat
				.format("SELECT DISTINCT DEVICE_MODEL AS DEVICE_MODEL FROM {0}.DEVICES WHERE DEVICE_MODEL IS NOT NULl AND APP_ID=:appId ORDER BY DEVICE_MODEL DESC",
						schema);
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		query.setParameter("appId", appId);
		List<Object> results = query.list();
		String[] strArry = new String[results.size()];
		for (int i = 0; i < results.size(); i++) {
			strArry[i] = (String) results.get(i);
		}
		return strArry;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getSysVersionsByModel(String deviceModel) {
		String schema = ResourceBundle.getBundle(
				GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault())
				.getString("pns.schema");
		String hql = MessageFormat
				.format("SELECT DISTINCT systemVersion FROM {0}.Devices WHERE deviceModel=:model AND systemVersion IS NOT NULL",
						schema);
		Session session = getSession();
		Query query = session.createQuery(hql);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDevicesModelsByType(String deviceType) {
		String schema = ResourceBundle.getBundle(
				GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault())
				.getString("pns.schema");
		String sql = MessageFormat
				.format("SELECT DISTINCT DEVICE_MODEL FROM {0}.DEVICES WHERE DEVICE_MODEL IS NOT NULl AND DEVICE_TYPE=:type ORDER BY DEVICE_MODEL DESC",
						schema);
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		query.setParameter("type", deviceType);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		return (List<Map<String, Object>>) query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getByOnlineUserId(Long userId) {
		String schema = ResourceBundle.getBundle(
				GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault())
				.getString("pns.schema");
		String sql = MessageFormat
				.format("SELECT d.* FROM {0}.DEVICES d INNER JOIN {0}.USER_DEVICES ud ON ud.DEVICE_ID=d.\"ID\" WHERE ud.USER_ID=:userId AND ud.STATUS=1",
						schema);
		Session session = getSession();
		SQLQuery query = session.createSQLQuery(sql);
		query.setParameter("userId", userId);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		return (List<Map<String, Object>>) query.list();
	}
}
